package com.yk.anima.servlet;

import com.alibaba.fastjson.JSON;
import com.yk.anima.dao.AdminMapper;
import com.yk.anima.exption.ExceptionCast;
import com.yk.anima.pojo.Admin;
import com.yk.anima.response.CommonCode;
import com.yk.anima.response.ResponseResult;
import com.yk.anima.util.MD5Utils;
import com.yk.anima.util.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;

/**
 * @author xiaokai
 * @create 2021-05-13 11:52:29
 * @desc...
 */
@Service
public class AdminServlet {

    @Autowired
    AdminMapper adminMapper;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    public Admin login(String userName, String passWord, String validateCode) {
        if (validateCode == null) {
            ExceptionCast.cast(CommonCode.VALIDATECODE);
        }
        HttpServletRequest request = this.getRequest();
        //获得客户端的ip地址 每一个客户端的ip地址都是相同的
        String ip = request.getRemoteAddr();
        String str = ip + validateCode;
        String code = stringRedisTemplate.opsForValue().get(str);
        if (!code.equals(validateCode)) {
            ExceptionCast.cast(CommonCode.USERNAME);
        }

        //e10adc3949ba59abbe56e057f20f883e
        if (userName == null || passWord == null) {
            //用户名或者密码不能为空
            ExceptionCast.cast(CommonCode.USERNAMEANDPASSWORD);
        }
        Admin admin = adminMapper.findByUserNameAndPassWord(userName, passWord);
        if (admin == null) {
            //用户名不存在
            ExceptionCast.cast(CommonCode.USERNAME);
        }
        String user = JSON.toJSONString(admin);
        //登录用户放到redis中 保存40分钟
        stringRedisTemplate.opsForValue().set(ip, user, 40, TimeUnit.MINUTES);
        return admin;
    }

    private HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.
                currentRequestAttributes()).getRequest();
    }


    public ResponseResult exit(int id) {

        Admin admin = adminMapper.selectByPrimaryKey(id);
        if (admin != null) {
            HttpServletRequest request = this.getRequest();
            //获得客户端的ip地址 每一个客户端的ip地址都是相同的
            String ip = request.getRemoteAddr();
            String user = stringRedisTemplate.opsForValue().get(ip);
            if (user==null){
                return new ResponseResult(CommonCode.USERNOLOGIN);
            }
            stringRedisTemplate.delete(ip);

            return new ResponseResult(CommonCode.SUCCESS);
        }

        return new ResponseResult(CommonCode.FAIL);

    }

    public ResponseResult registration(Admin admin) {
        if (admin == null) {
            return new ResponseResult(CommonCode.FAIL);
        }
        String phone = admin.getPhone();
        Admin am = adminMapper.findByPhone(phone);
        if (am != null) {
            return new ResponseResult(CommonCode.PHONEISNULL);
        }
        Admin newAdmin = new Admin();
        newAdmin.setAddress(admin.getAddress());
        newAdmin.setAge(admin.getAge());
        newAdmin.setaId(null);
        newAdmin.setGender(admin.getGender());
        newAdmin.setPassWord(MD5Utils.md5(admin.getPassWord()));

        newAdmin.setPhone(admin.getPhone());
        newAdmin.setUserName(admin.getUserName());
        int number = adminMapper.insertSelective(newAdmin);
        if (number > 0) {
            return new ResponseResult(CommonCode.SUCCESS);
        }
        return new ResponseResult(CommonCode.FAIL);
    }

    public String add() {

        HttpServletRequest request = this.getRequest();
        String ip = request.getRemoteHost();
        //生成6位数验证码
        String validateCode = String.valueOf(ValidateCodeUtils.generateValidateCode(6));
        //有效期1分钟
        stringRedisTemplate.opsForValue().set(ip + validateCode, validateCode, 1, TimeUnit.MINUTES);
        return validateCode;
    }
}
